﻿using System;
using System.IO;
using System.Text;

namespace NumericalSolution {
  class Poisson1DFEMMain {

    static String outfile = "c:/nsworkspace/poisson1d_fem.csv";
    static double beta = 2.0;

    public static void Main() {

      Poisson1DFEM problem = new Poisson1DFEM();
      problem.NumberOfElements = 10;
      problem.NumberOfNodesInAnElement = 2;
      problem.NumericalIntegration = new GaussLagrange(2);
      problem.Beta = beta;

      problem.CreateMesh();
      problem.AssembleMatrix();
      
      
      Node[] result = problem.Solve();

      StreamWriter writer = File.CreateText(outfile);
      writer.WriteLine("x" + "," + "exact" + "," + "numerical");
      for (int i = 0; i < result.Length; i++) {
        writer.Write(result[i].X + ", ");
        writer.Write(exact(result[i].X) + ", ");
        writer.WriteLine(result[i].Value);
      }
      writer.Close();
    }

    public static double exact(double x) {
      return -0.5 * beta * x * x + (0.5 * beta + 1) * x;
    }
  }
}
